using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using OlympusSoftware.Technical.Globalize.BusinessLogic;
using OlympusSoftware.Technical.Globalize.BusinessEntities;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Diagnostics;

namespace PortugalPremiumRealEstate
{
    public partial class _Default : System.Web.UI.Page
    {
        GlobalizeManager gm = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            gm = base.Application["GlobalizeManager"] as GlobalizeManager;
            this.BuildLanguageMenu(gm);
            this.BuildTexts(gm);
        }

        protected void Menu_Click(object sender, EventArgs e)
        {
            HtmlAnchor menu = sender as HtmlAnchor;
            headermaintext.InnerHtml = menu.InnerHtml;
            int menuId = int.Parse(menu.Name);
            maintext.InnerHtml = gm.GetText(2, int.Parse(menu.Name));

            if (menuId == 10)
            {
                maintext.InnerHtml = RenderOpportunities();
            }
        }

        public static string RenderOpportunities()
        {
            GlobalizeManager gm = HttpContext.Current.Application["GlobalizeManager"] as GlobalizeManager;
            Database database = DatabaseFactory.CreateDatabase("OlympusSoftware");
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[GetOpportunities]");
            string maintextInnerHtml = string.Empty;
            int lastOppId = -1;
            using (IDataReader reader = database.ExecuteReader(storedProcCommand))
            {
                while (reader.Read())
                {
                    /*if (lastOppId != reader.GetInt32(0))
                    {
                        lastOppId = reader.GetInt32(0);
                        maintext.InnerHtml += "<p><b>" + gm.GetText(2, reader.GetInt32(1)) + "</b></p><p>" + gm.GetText(2, reader.GetInt32(2)) + "</p>";
                    }

                    maintext.InnerHtml += "<a href=\"GetImage.aspx?idImg=" + reader.GetInt32(4).ToString() + "\"><img width=\"100px\" height=\"100px\" src=\"GetImage.aspx?idImg=" + reader.GetInt32(4).ToString() + "\" /></a>";
                     */
                    if (lastOppId != reader.GetInt32(0))
                    {
                        lastOppId = reader.GetInt32(0);
                        maintextInnerHtml += "<p><a href=\"ShowOpportunity.aspx?oppId=" + reader.GetInt32(0).ToString() + "\"><img width=\"70px\" height=\"70px\" src=\"GetImage.aspx?idImg=" + reader.GetInt32(2).ToString() + "\" /> " + gm.GetText(2, reader.GetInt32(1)) + "</a></p>";
                    }
                }
            }
            return maintextInnerHtml; 
        }

        private void BuildTexts(GlobalizeManager gm)
        {
            
            title.InnerText = gm.GetText(2, 1);

            menu1.InnerText = gm.GetText(2, 2);
            menu2.InnerText = gm.GetText(2, 3);
            menu3.InnerText = gm.GetText(2, 4);
            menu4.InnerText = gm.GetText(2, 5);
            menu5.InnerText = gm.GetText(2, 6);

            headermaintext.InnerHtml = gm.GetText(1, 4);
            maintext.InnerHtml = gm.GetText(1, 2);
            
        }

        private void BuildLanguageMenu(GlobalizeManager gm)
        {
            List<Language> linguas = gm.GetLanguages();
            foreach (Language lingua in linguas)
            {
                ImageButton img = new ImageButton();
                img.ImageUrl = string.Format("images/flags/{0}.gif", lingua.Code);
                img.Style["margin-right"] = "5px";
                img.ID = lingua.ID.ToString();
                img.Command += new CommandEventHandler(this.Lingua_Command);
                this.langMenu.Controls.Add(img);
            }
        }

        private void Lingua_Command(object sender, CommandEventArgs e)
        {
            GlobalizeManager gm = base.Application["GlobalizeManager"] as GlobalizeManager;
            gm.Language = int.Parse(((ImageButton)sender).ID);
            this.BuildTexts(gm);
        }


    }
}
